//
//  GTBarButtonWithState.m
//  gcast-toolbox4iphone
//

#import "GTBarPlainButtonWithState.h"
#import "UIImage+GTExtension.h"

@interface GTBarButtonWithUIPlainButton ()
@property(nonatomic,retain,readonly) UIButton *button;
@end

@interface GTBarPlainButtonWithState ()
@property(nonatomic,retain,readwrite) UIImage *originalImage;
@property(nonatomic,retain,readwrite) UIColor *colorForActiveState;
@property(nonatomic,assign,readwrite) BOOL active;
- (UIImage*)imageToShow;
@end

@implementation GTBarPlainButtonWithState
@synthesize originalImage, colorForActiveState, active;

- (id)initWithImage:(UIImage*)image colorForActiveState:(UIColor*)theColorForActiveState {
  if ((self = [super initWithImage:image])) {
    self.originalImage = image;
    self.colorForActiveState = theColorForActiveState;
    [self.button setImage:[self imageToShow] forState:UIControlStateNormal];  
  }
  return self;
}

- (void)dealloc {
  [originalImage release];
  [colorForActiveState release];
  [super dealloc];
}

- (UIImage*)imageToShow {
  UIImage* result = nil;
  if (self.active) {
    result = [self.originalImage paintWithColor:self.colorForActiveState];
  }
  else {
//    result = [[[UIImage alloc] initWithCGImage:[self.originalImage CGImage]] autorelease];
    result = [self.originalImage paintWithColor:[UIColor whiteColor]];
  }
  return result;
}

- (void)activate {
  self.active = TRUE;
  [self.button setImage:[self imageToShow] forState:UIControlStateNormal];  
}

- (void)deactivate {
  self.active = FALSE;
  [self.button setImage:[self imageToShow] forState:UIControlStateNormal];  
}

@end
